import flet as ft
import sys

# 导入必要的模块
def main(page: ft.Page):
    # 设置页面标题
    page.title = "SiliconFlow 图像生成器"
    
    # 添加简单的欢迎信息
    page.add(ft.Text("欢迎使用SiliconFlow图像生成器", size=24, weight="bold"))
    page.add(ft.Text("正在加载应用界面..."))
    
    try:
        # 尝试导入并使用原始的UI组件
        from modules.config import get_config
        from modules.ui import create_ui
        from modules.utils import setup_logging
        
        # 设置日志
        logger = setup_logging()
        config = get_config()
        
        # 设置主题
        page.theme_mode = config["theme_mode"]
        
        # 清除临时内容
        page.controls.clear()
        
        # 创建并添加实际的UI
        ui_content = create_ui(page, config, logger)
        page.add(ui_content)
        
    except Exception as e:
        # 如果出错，显示错误信息
        error_text = f"加载应用时出错: {str(e)}"
        print(error_text)
        page.add(ft.Text(error_text, color=ft.colors.RED))

if __name__ == "__main__":
    print("启动SiliconFlow图像生成器(Web模式)...")
    print(f"Python版本: {sys.version}")
    
    try:
        # 尝试以web模式启动
        ft.app(target=main, view=ft.WEB_BROWSER)
    except Exception as e:
        print(f"启动失败: {str(e)}")
        import traceback
        traceback.print_exc()
        input("按回车键退出...")